/**
 * Created by zlj on 2016/12/6.
 */
require('jquery')
require('layout/util');
var $form = require('js-path/formValide.js');
var $alert = require('libs/alert');
$(function () {
    $('#channel_back').on('click',function () {
        window.history.go(-1);
    });
    judge();
    formValideinit();
    $('#submit_form').on('click',function () {
        $('#logistics_form').submit();
    })
});
//表单验证
function formValideinit() {
    $form({selector:"#logistics_form"},{
        code:{required:true,hasSpecial:true,remote:{url:'api/v1/p/mms/domesticLogisticsProvider/isExsit', type: "post",
            data: { code : function(){ return $("#logistics_form input[name=code]").val()}, id : function(){ return $("#logistics_form input[name=id]").val()}},dataFilter:function (result) {
                var  obj = JSON.parse(result);
                if(obj.code == '200' ){
                    if(!obj.data){return true} else { return false}
                }else{
                    return false;
                }
            }
        }},name:{required:true},linkman:{required:true},linkTelphone:{required:true},
        address:{required:true}
    },{code:{required:'物流渠道简称必填',remote:'国际物流渠道简称已存在'},name:{required:'国内物流渠道名称必填',linkman:{required:'联系人必填'}
    ,linkTelphone:{required:'联系电话必填'},address:{required:'联系地址必填'}}
    },function () {
         return $('#logistics_form').serializeObject();
    },function (result) {
        if(result.code == 200){
            window.location.href = 'Logistics-china-list';
        }else{
            $alert(result.message);
        }
    })
};
//judge判断新增或更新
function judge() {
    var url =window.location.href;
    if(url.indexOf("id=")>0){
        $('#logistics_form').attr('action','api/v1/p/mms/domesticLogisticsProvider/update')
        $('span.name:first').html('物流渠道更新');
        $('#submit_form').html('更新保存');
        var id = $.getUrlParam('id');
        load(id);
    }
}

//load
function load(id) {
    $.ajax({
        url:'api/v1/p/mms/domesticLogisticsProvider/detail',
        type:'post',
        data:{
            id:id
        },
        dataType:'json',
        success:function (result) {
            if (result.code == 200){
                $('#logistics_form').setForm(result.data);
            }else{
                $alert(result.message);
            }
        },
        error:function (e) {
            console.error('api/v1/p/mms/domesticLogisticsProvider/detail 接口调用失败 ' )
        }
    })
}
